package l1j.server.server.model.damage.hit;

import l1j.server.server.ActionCodes;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.npc.action.L1NpcDefaultAction;
import l1j.server.server.serverpackets.S_AttackPacket;
import l1j.server.server.serverpackets.S_UseArrowSkill;
import l1j.server.server.types.Point;
public class L1NpcHit {
	private L1NpcInstance _attacker;
	private L1Character _target;

	public L1NpcHit(L1NpcInstance attacker, L1Character target) {
		_attacker = attacker;
		_target = target;
	}

	public int calcNpcHitRate(int _hitRate) {
		_hitRate += _attacker.getLevel();

		if (_attacker instanceof L1PetInstance) { // ペットの武器による追加命中
			_hitRate += ((L1PetInstance) _attacker).getHitByWeapon();
		}

		_hitRate += _attacker.getHitup();
		return _hitRate;
	}

	public void doAttackAction() {
		int bowActId = 0;
		int npcGfxid = _attacker.getTempCharGfx();
		int actId = L1NpcDefaultAction.getInstance().getSpecialAttack(npcGfxid); // 特殊攻擊動作
		int _targetX = _target.getX();
		int _targetY = _target.getY();
		int _targetId = _target.getId();
		int[] data = null;

		_attacker.setHeading(_attacker.targetDirection(_targetX, _targetY)); // 改變面向

		// 與目標距離2格以上
		boolean isLongRange = false;
		if (npcGfxid == 4521 || npcGfxid == 4550 || npcGfxid == 5062
				|| npcGfxid == 5317 || npcGfxid == 5324 || npcGfxid == 5331
				|| npcGfxid == 5338 || npcGfxid == 5412) {
			isLongRange = (_attacker.getLocation().getTileLineDistance(
					new Point(_targetX, _targetY)) > 2);
		} else {
			isLongRange = (_attacker.getLocation().getTileLineDistance(
					new Point(_targetX, _targetY)) > 1);
		}
		bowActId = _attacker.getPolyArrowGfx(); // 被變身後的遠距圖像
		if (bowActId == 0) {
			bowActId = _attacker.getNpcTemplate().getBowActId();
		}
		if (_attacker.getCurrentAttackActId() == ActionCodes.ACTION_Attack) {

			if (!isLongRange || bowActId == 0) { // 近距離
				actId = L1NpcDefaultAction.getInstance().getDefaultAttack(
						npcGfxid);
			} else { // 遠距離
				actId = L1NpcDefaultAction.getInstance().getRangedAttack(
						npcGfxid);
			}

		} else {
			actId = _attacker.getCurrentAttackActId();// 攻擊動作由 mobskill控制
		}

		// 距離2格以上攻使用 弓 攻擊
		if (isLongRange && (bowActId > 0)) {
			data = new int[] { actId, 0, bowActId }; // data = {actid,
														// dmg, spellgfx}
			_attacker.broadcastPacket(new S_UseArrowSkill(_attacker, _targetId,
					_targetX, _targetY, data));
		} else {
			data = new int[] { actId, 0, 0 }; // data = {actid, dmg,
												// effect}
			_attacker.broadcastPacket(new S_AttackPacket(_attacker, _targetId,
					data));

		}
	}
}
